package portlet;

import java.io.IOException;

import java.util.StringTokenizer;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import javax.faces.model.SelectItem;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import oracle.jbo.common.JboCompOper;
import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;


public class danhsachthutuc extends ADFBridgePortlet {
    //Variables
    private Object urlchitiet;
    private Object urldanhsach;
    private Object langid;
    private Object danhmucid;
    private Object coquanid;
    private Object linhvucid;
    private Object tukhoa;
    private Object tendanhmuc;
    private Object tencoquan;
    private Object tenlinhvuc;
    private String viewRender;
    //Labels
    private String labelPortlet;
    private String labelTukhoa;
    private String labelDanhmuc;
    private String labelCoquan;
    private String labelLinhvuc;
    private String labelTimkiem;
    private String labelXemtatca;
    private String labelStt;
    private String labelNoData;
    private String labelNoAccess;
    private String labelTieude;
    private String labelHethieuluc;
    private String labelChontatca;
    private String labelTrangdau;
    private String labelTrangtruoc;
    private String labelTrangsau;
    private String labelTrangcuoi;
    //
    private String coquanSelectedValue;
    private SelectItem[] coquanElements = null;
    private String linhvucSelectedValue;
    private SelectItem[] linhvucElements = null;
    private RichSelectOneChoice coquanSelectChoice;
    private RichSelectOneChoice linhvucSelectChoice;
    //Paging
    private boolean paging1 = false;
    private boolean paging2 = false;
    private int pageNumber;
    private int pageNumber2;
    private static final int RANGE_SIZE = 10;

    public danhsachthutuc() {

    }

    public String getViewRender() {
        getLangid();
        getDanhmucid();
        getCoquanid();
        getLinhvucid();
        getThutucHanhchinh();
        return viewRender;
    }

    private void getThutucHanhchinh() {
        try {
            tukhoa = Utils.getPortletRequest().getParameter("tukhoa");
        } catch (Exception e) {
            e.printStackTrace();
        }
        getThutucByViewCriteria("TthcThutucView1Iterator",1);
        getThutucByViewCriteria("TthcHetHieuLuc1Iterator", 0);
        checkPaging();
        tendanhmuc = Utils.getResource("DANH_SACH_THU_TUC");
        //AdfFacesContext.getCurrentInstance().addPartialTarget(table);
    }

    private void getThutucByViewCriteria(String IteratorName, int HieuLuc) {
        DCIteratorBinding thutucIterator =
            Utils.getIteratorBinding(IteratorName);
        ViewObject vo = thutucIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("VC" + IteratorName);
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        if (danhmucid != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem1 =
                vcRow.ensureCriteriaItem("DanhmucId");
            rowViewCriteriaItem1.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem1.setValue(danhmucid);

            DCIteratorBinding danhmucIterator =
                Utils.getIteratorBinding("TthcDanhmucView1Iterator");
            ViewObject voDanhmuc = danhmucIterator.getViewObject();
            ViewCriteria vc2 = voDanhmuc.createViewCriteria();
            ViewCriteriaRow cr2 = vc2.createViewCriteriaRow();
            String varStr2 = "=" + danhmucid;
            cr2.setAttribute("Id", varStr2);
            vc2.addElement(cr2);
            voDanhmuc.getViewCriteriaManager().clearViewCriterias();
            voDanhmuc.applyViewCriteria(vc2);
            voDanhmuc.executeQuery();
            if (voDanhmuc.getAllRowsInRange().length > 0) {
                tendanhmuc =
                        voDanhmuc.getRowAtRangeIndex(0).getAttribute("Tieude");
            }
        }
        if (coquanid != null && coquanid.equals("all") == false) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("CoquanId");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_CONTAINS_DELIMITED_ID);
            rowViewCriteriaItem2.setValue(coquanid);
            coquanSelectChoice.setValue(coquanid);
        }
        else{coquanSelectChoice.setValue(null);}
        if (linhvucid != null && linhvucid.equals("all") == false) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem3 =
                vcRow.ensureCriteriaItem("LinhvucId");
            rowViewCriteriaItem3.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem3.setValue(linhvucid);
            linhvucSelectChoice.setValue(linhvucid);
        }
        else{linhvucSelectChoice.setValue(null);}
        if (tukhoa != null || tukhoa != "") {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem4 =
                vcRow.ensureCriteriaItem("Tieude");
            rowViewCriteriaItem4.setOperator(JboCompOper.OPER_CONTAINS);
            rowViewCriteriaItem4.setValue(tukhoa);
        }
        if(HieuLuc != 1){
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem6 =
                vcRow.ensureCriteriaItem("HieuLuc");
            rowViewCriteriaItem6.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem6.setValue(0);
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("VC" +
                                                                IteratorName);
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if(vo.getAllRowsInRange().length>0){
//            FacesContext context = FacesContext.getCurrentInstance();
//            ExtendedRenderKitService extRenderKitSrvc =
//                Service.getRenderKitService(context,
//                                            ExtendedRenderKitService.class);
//            extRenderKitSrvc.addScript(context, "autoResize();");
        }
    }

    public void timkiem() {
        getUrldanhsach();
        if (coquanSelectedValue == null) {
            coquanSelectedValue = "all";
        }
        if (linhvucSelectedValue == null) {
            linhvucSelectedValue = "all";
        }
        Utils.redirect(urldanhsach + "?coquan=" + coquanSelectedValue +
                       "&linhvuc=" + linhvucSelectedValue + "&tukhoa=" +
                       tukhoa);
    }

    public void coquanValueChanged(ValueChangeEvent valueChangeEvent) {
        coquanSelectedValue = valueChangeEvent.getNewValue().toString();
    }

    public void linhvucValueChanged(ValueChangeEvent valueChangeEvent) {
        linhvucSelectedValue = valueChangeEvent.getNewValue().toString();
    }

    public Object getUrlchitiet() {
        try {
            urlchitiet = Utils.getPortletRequest().getParameter("urlchitiet");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlchitiet;
    }

    public Object getUrldanhsach() {
        try {
            urldanhsach =
                    Utils.getPortletRequest().getParameter("urldanhsach");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urldanhsach;
    }

    public Object getLangid() {
        try {
            langid = Utils.getPortalLocale().getLanguage();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return langid;
    }

    public Object getDanhmucid() {
        try {
            danhmucid = Utils.getPortletRequest().getParameter("danhmuc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return danhmucid;
    }

    public Object getCoquanid() {
        try {
            coquanid = Utils.getPortletRequest().getParameter("coquan");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return coquanid;
    }

    public Object getLinhvucid() {
        try {
            linhvucid = Utils.getPortletRequest().getParameter("linhvuc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return linhvucid;
    }

    public void setTukhoa(Object tukhoa) {
        this.tukhoa = tukhoa;
    }

    public Object getTukhoa() {
        try {
            tukhoa = Utils.getPortletRequest().getParameter("tukhoa");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (tukhoa == null) {
            tukhoa = "";
        }
        return tukhoa;
    }

    //FOR THU TUC PAGING

    public String goLastPage() {
        Utils.goLastPage(RANGE_SIZE, "TthcThutucView1");
        return null;
    }

    public boolean isLastEnabled() {
        return Utils.isLastEnabled(RANGE_SIZE, "TthcThutucView1");
    }

    public boolean isFirstEnabled() {
        return Utils.isFirstEnabled(RANGE_SIZE, "TthcThutucView1");
    }

    public void setPageNumber(int pageNumber) {
        JUCtrlRangeBinding thutucView = this.getThutucView();
        if (pageNumber >
            Math.max(1, (thutucView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumber =
                    (int)Math.max(1, (thutucView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumber = pageNumber;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    private JUCtrlRangeBinding getThutucView() {
        return Utils.getViewObject("TthcThutucView1");
    }

    private void checkPaging() {
        long lastPageNum1 =
            Math.max(1, (getThutucView().getEstimatedRowCount() - 1) /
                     RANGE_SIZE + 1);
        long lastPageNum2 =
            Math.max(1, (getThutucView2().getEstimatedRowCount() - 1) /
                     RANGE_SIZE + 1);
        if (lastPageNum1 > 1) {
            setPaging1(true);
        }
        if (lastPageNum2 > 1) {
            setPaging2(true);
        }
    }

    //FOR THU TUC HET HIEU LUC

    public String goLastPage2() {
        Utils.goLastPage(RANGE_SIZE, "TthcHetHieuLuc1");
        return null;
    }

    public boolean isFirstEnabled2() {
        return Utils.isFirstEnabled(RANGE_SIZE, "TthcHetHieuLuc1");
    }

    public boolean isLastEnabled2() {
        return Utils.isLastEnabled(RANGE_SIZE, "TthcHetHieuLuc1");
    }

    public void setPageNumber2(int pageNumber2) {
        JUCtrlRangeBinding thutucView = this.getThutucView2();
        if (pageNumber2 >
            Math.max(1, (thutucView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumber2 =
                    (int)Math.max(1, (thutucView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumber2 = pageNumber2;
    }

    public int getPageNumber2() {
        return pageNumber2;
    }

    private JUCtrlRangeBinding getThutucView2() {
        return Utils.getViewObject("TthcHetHieuLuc1");
    }

    public void setLabelXemtatca(String labelXemtatca) {
        this.labelXemtatca = labelXemtatca;
    }

    public void setPaging1(boolean paging1) {
        this.paging1 = paging1;
    }

    public boolean isPaging1() {
        return paging1;
    }

    public void setPaging2(boolean paging2) {
        this.paging2 = paging2;
    }

    public boolean isPaging2() {
        return paging2;
    }

    public void setCoquanSelectedValue(String coquanSelectedValue) {
        this.coquanSelectedValue = coquanSelectedValue;
    }

    public String getCoquanSelectedValue() {
        return coquanSelectedValue;
    }

    public void setCoquanElements(SelectItem[] coquanElements) {
        this.coquanElements = coquanElements;
    }

    public SelectItem[] getCoquanElements() {
        DCIteratorBinding coquanIterator =
            Utils.getIteratorBinding("TthcCoquanView1Iterator");
        ViewObject vo = coquanIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "=" + langid;
        cr.setAttribute("NgonNgu", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        int length = vo.getAllRowsInRange().length;
        coquanElements = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = vo.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            coquanElements[i] = item;
        }
        return coquanElements;
    }

    public void setLinhvucSelectedValue(String linhvucSelectedValue) {
        this.linhvucSelectedValue = linhvucSelectedValue;
    }

    public String getLinhvucSelectedValue() {
        return linhvucSelectedValue;
    }

    public void setLinhvucElements(SelectItem[] linhvucElements) {
        this.linhvucElements = linhvucElements;
    }

    public SelectItem[] getLinhvucElements() {
        DCIteratorBinding coquanIterator =
            Utils.getIteratorBinding("TthcLinhvucView1Iterator");
        ViewObject vo = coquanIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "=" + langid;
        cr.setAttribute("NgonNgu", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        int length = vo.getAllRowsInRange().length;
        linhvucElements = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = vo.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            linhvucElements[i] = item;
        }
        return linhvucElements;
    }

    public void setCoquanSelectChoice(RichSelectOneChoice coquanSelectChoice) {
        this.coquanSelectChoice = coquanSelectChoice;
    }

    public RichSelectOneChoice getCoquanSelectChoice() {
        return coquanSelectChoice;
    }

    public void setLinhvucSelectChoice(RichSelectOneChoice linhvucSelectChoice) {
        this.linhvucSelectChoice = linhvucSelectChoice;
    }

    public RichSelectOneChoice getLinhvucSelectChoice() {
        return linhvucSelectChoice;
    }

    public Object getTendanhmuc() {
        return tendanhmuc;
    }

    public Object getTencoquan() {
        return tencoquan;
    }

    public Object getTenlinhvuc() {
        return tenlinhvuc;
    }

    public String getLabelPortlet() {
        try {
            labelPortlet =
                    Utils.getPortletRequest().getParameter("tieudeportlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (labelPortlet == null) {
            labelPortlet = Utils.getResource("THU_TUC_HANH_CHINH");
        }
        return labelPortlet;
    }

    public String getLabelTukhoa() {
        labelTukhoa = Utils.getResource("TU_KHOA");
        return labelTukhoa;
    }

    public String getLabelDanhmuc() {
        labelDanhmuc = Utils.getResource("TIEU_DE_DANH_MUC");
        return labelDanhmuc;
    }

    public String getLabelCoquan() {
        labelCoquan = Utils.getResource("CO_QUAN_THUC_HIEN");
        return labelCoquan;
    }

    public String getLabelLinhvuc() {
        labelLinhvuc = Utils.getResource("LINH_VUC_THUC_HIEN");
        return labelLinhvuc;
    }

    public String getLabelTimkiem() {
        labelTimkiem = Utils.getResource("TIM_KIEM");
        return labelTimkiem;
    }

    public String getLabelXemtatca() {
        labelXemtatca = Utils.getResource("XEM_TAT_CA");
        return labelXemtatca;
    }

    public String getLabelStt() {
        labelStt = Utils.getResource("SO_THU_TU");
        return labelStt;
    }

    public String getLabelNoData() {
        labelNoData = Utils.getResource("NO_DATA");
        return labelNoData;
    }

    public String getLabelNoAccess() {
        labelNoAccess = Utils.getResource("ACCESS_DENIED");
        return labelNoAccess;
    }

    public String getLabelTieude() {
        labelTieude = Utils.getResource("TIEU_DE");
        return labelTieude;
    }

    public String getLabelHethieuluc() {
        labelHethieuluc = Utils.getResource("THU_TUC_HET_HIEU_LUC");
        return labelHethieuluc;
    }

    public String getLabelChontatca() {
        labelChontatca = Utils.getResource("CHON_TAT_CA");
        return labelChontatca;
    }

    public String getLabelTrangdau() {
        labelTrangdau = Utils.getResource("TRANG_DAU");
        return labelTrangdau;
    }

    public String getLabelTrangtruoc() {
        labelTrangtruoc = Utils.getResource("TRANG_TRUOC");
        return labelTrangtruoc;
    }

    public String getLabelTrangsau() {
        labelTrangsau = Utils.getResource("TRANG_SAU");
        return labelTrangsau;
    }

    public String getLabelTrangcuoi() {
        labelTrangcuoi = Utils.getResource("TRANG_CUOI");
        return labelTrangcuoi;
    }
}
